package org.example.managementsystem.repository;

import org.example.managementsystem.model.DeviceScrap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * 设备报废数据访问对象
 */
@Repository
public class DeviceScrapDao {
    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public DeviceScrapDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 插入设备报废申请
     */
    public int insertScrap(DeviceScrap scrap) {
        String sql = "INSERT INTO device_scrap_application (repair_id, is_approved, status, description) " +
                "VALUES (?, ?, ?, ?)";
        jdbcTemplate.update(sql,
                scrap.getRepairId(),
                scrap.getIsApproved(),
                scrap.getStatus(),
                scrap.getDescription());
        // 获取刚刚插入的 id
        String getLastIdSql = "SELECT LAST_INSERT_ID()";
        int lastInsertId = jdbcTemplate.queryForObject(getLastIdSql, Integer.class);
        return lastInsertId;
    }

    /**
     * 更新设备报废状态
     */
    public int updateScrapStatus(int scrapId, boolean isApproved, String description) {
        String sql = "UPDATE device_scrap_application SET is_approved = ?, description = ? WHERE scrap_id = ?";
        return jdbcTemplate.update(sql, isApproved, description, scrapId);
    }

    public int updateScrapStatus(int scrapId, boolean isApproved, boolean status) {
        String sql = "UPDATE device_scrap_application SET is_approved = ?, status = ? WHERE scrap_id = ?";
        return jdbcTemplate.update(sql, isApproved, status, scrapId);
    }

    public int updateScrapStatus(int scrapId, boolean isApproved) {
        String sql = "UPDATE device_scrap_application SET is_approved = ? WHERE scrap_id = ?";
        return jdbcTemplate.update(sql, isApproved, scrapId);
    }

    public int updateScrap(DeviceScrap scrap){
        String sql = "UPDATE device_scrap_application SET repair_id = ?, description = ?, is_approved = ?, status = ? WHERE scrap_id = ?";
        return jdbcTemplate.update(sql, scrap.getRepairId(), scrap.getDescription(), scrap.getIsApproved(), scrap.getStatus(), scrap.getScrapId());
    }

    /**
     * 查询所有报废申请
     */
    public List<DeviceScrap> getAllScraps() {
        String sql = "SELECT * FROM device_scrap_application";
        return jdbcTemplate.query(sql, new DeviceScrapRowMapper());
    }

    /**
     * 查询所有未审核的报废申请
     */
    public List<DeviceScrap> getAllUnreviewedScraps() {
        String sql = "SELECT * FROM device_scrap_application WHERE status = 0";
        return jdbcTemplate.query(sql, new DeviceScrapRowMapper());
    }

    /**
     * 根据ID查询报废申请
     */
    public DeviceScrap getScrapById(int scrapId) {
        String sql = "SELECT * FROM device_scrap_application WHERE scrap_id = ?";
        List<DeviceScrap> scraps = jdbcTemplate.query(sql, new DeviceScrapRowMapper(), scrapId);
        return scraps.isEmpty() ? null : scraps.get(0);
    }

    /**
     * 根据报修ID查询报废申请
     */
    public List<DeviceScrap> getScrapByRepairId(int repairId) {
        String sql = "SELECT * FROM device_scrap_application WHERE repair_id = ?";
        return jdbcTemplate.query(sql, new DeviceScrapRowMapper(), repairId);
    }

    /**
     * 报废申请行映射器
     */
    private static class DeviceScrapRowMapper implements RowMapper<DeviceScrap> {
        @Override
        public DeviceScrap mapRow(ResultSet rs, int rowNum) throws SQLException {
            DeviceScrap scrap = new DeviceScrap();
            scrap.setScrapId(rs.getInt("scrap_id"));
            scrap.setRepairId(rs.getInt("repair_id"));
            scrap.setIsApproved(rs.getBoolean("is_approved"));
            scrap.setStatus(rs.getBoolean("status"));
            scrap.setDescription(rs.getString("description"));
            
            return scrap;
        }
    }

    /**
     * 根据ID删除报废申请
     */
    public int deleteScrap(int scrapId) {
        String sql = "DELETE FROM device_scrap_application WHERE scrap_id = ?";
        return jdbcTemplate.update(sql, scrapId);
    }
}